package cn.zhqr.common.util.sql;

import java.util.Collection;
import java.util.Iterator;

/**
 * 用于拼接sql语句
 */
public class SqlBuilder {
    public SqlBuilder() {}

    /**
     * 将数组转换为sql集合，形如：(x,x,x)
     * @param set
     * @return
     */
    public static String buildSqlList(Collection set) {
        if (set != null && !set.isEmpty()) {
            StringBuilder sb = new StringBuilder("(");
            Iterator i = set.iterator();
            String quote;

            while(i.hasNext()) {
                Object o = i.next();
                if (o.toString().contains("'")) {
                    quote = "''";
                } else {
                    quote = "'";
                }

                sb.append(quote).append(o).append(quote);
                if (i.hasNext()) {
                    sb.append(",");
                }
            }

            sb.append(")");
            return sb.toString();
        } else {
            return "(NULL)";
        }
    }
}
